package com.jsh.erp.controller;

/**
 * @author hanle
 * @description
 * @date 2025/6/8 11:11
 * @Company 西安图迹信息科技有限公司
 */

import com.jsh.erp.base.BaseController;
import com.jsh.erp.base.TableDataInfo;
import com.jsh.erp.datasource.entities.Prescription;
import com.jsh.erp.service.PrescriptionService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.ResponseJsonUtil;
import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/prescription")
@Api(tags = {"处方管理"})
public class PrescriptionController extends BaseController {
   private Logger logger = LoggerFactory.getLogger(PrescriptionController.class);

   @Resource
   private PrescriptionService prescriptionService;

   /**
    * 根据ID获取处方信息
    *
    * @param id      处方ID
    * @param request HTTP请求
    * @return JSON格式的处方信息
    * @throws Exception
    */
   @GetMapping("/info")
   @ApiOperation(value = "根据ID获取处方信息")
   public String getPrescriptionById(@RequestParam("id") Long id, HttpServletRequest request) throws Exception {
      Prescription prescription = prescriptionService.getPrescription(id);
      Map<String, Object> responseMap = new HashMap<>();

      if(prescription != null) {
         responseMap.put("info", prescription);
         return ResponseJsonUtil.returnJson(responseMap, ErpInfo.OK.name, ErpInfo.OK.code);
      }
      else {
         return ResponseJsonUtil.returnJson(responseMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
      }
   }

   /**
    * 获取处方信息列表
    *
    * @param search  搜索条件（包含客户ID和类型）
    * @param request HTTP请求
    * @return 表格数据信息
    * @throws Exception
    */
   @GetMapping("/list")
   @ApiOperation(value = "获取处方信息列表")
   public TableDataInfo getPrescriptionList(@RequestParam(value = Constants.SEARCH, required = false) String search,
                                            HttpServletRequest request) throws Exception
   {

      Long customerId = null;

      if(search != null && !search.isEmpty()) {
         if(StringUtil.isNotEmpty(StringUtil.getInfo(search, "customerId"))) {
            customerId = Long.parseLong(StringUtil.getInfo(search, "customerId"));
         }
      }

      List<Prescription> prescriptionList = prescriptionService.searchPrescriptions(customerId);
      return getDataTable(prescriptionList);
   }

   /**
    * 新增处方信息
    *
    * @param prescription 处方信息
    * @param request      HTTP请求
    * @return 操作结果
    * @throws Exception
    */
   @PostMapping("/add")
   @ApiOperation(value = "新增处方信息")
   public String addPrescription(@RequestBody Prescription prescription, HttpServletRequest request) throws Exception {
      int result = prescriptionService.createPrescription(prescription);
      return ResponseJsonUtil.returnStr(new HashMap<>(), result);
   }

   /**
    * 更新处方信息
    *
    * @param prescription 处方信息
    * @param request      HTTP请求
    * @return 操作结果
    * @throws Exception
    */
   @PutMapping("/update")
   @ApiOperation(value = "更新处方信息")
   public String updatePrescription(@RequestBody Prescription prescription, HttpServletRequest request) throws Exception
   {
      int result = prescriptionService.updatePrescription(prescription);
      return ResponseJsonUtil.returnStr(new HashMap<>(), result);
   }

   /**
    * 删除处方信息
    *
    * @param id      处方ID
    * @param request HTTP请求
    * @return 操作结果
    * @throws Exception
    */
   @DeleteMapping("/delete")
   @ApiOperation(value = "删除处方信息")
   public String deletePrescription(@RequestParam("id") Long id, HttpServletRequest request) throws Exception {
      int result = prescriptionService.deletePrescription(id);
      return ResponseJsonUtil.returnStr(new HashMap<>(), result);
   }

   /**
    * 批量删除处方信息
    *
    * @param ids     处方ID列表
    * @param request HTTP请求
    * @return 操作结果
    * @throws Exception
    */
   @DeleteMapping("/deleteBatch")
   @ApiOperation(value = "批量删除处方信息")
   public String batchDeletePrescriptions(@RequestParam("ids") List<Long> ids, HttpServletRequest request)
      throws Exception
   {
      int result = prescriptionService.batchDeletePrescriptions(ids);
      return ResponseJsonUtil.returnStr(new HashMap<>(), result);
   }

   /**
    * 获取客户的最新处方信息
    *
    * @param customerId 客户ID
    * @param request    HTTP请求
    * @return 最新处方信息
    * @throws Exception
    */
   @GetMapping("/latest")
   @ApiOperation(value = "获取客户的最新处方信息")
   public String getLatestPrescription(@RequestParam("customerId") Long customerId, HttpServletRequest request)
      throws Exception
   {
      Prescription latestPrescription = prescriptionService.getLatestPrescription(customerId);

      Map<String, Object> objectMap = new HashMap<>();
      if(latestPrescription != null) {
         objectMap.put("latestPrescription", latestPrescription);
         return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
      }
      else {
         return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
      }
   }
}
